//
//  PKNetworkUtils_Alamofire.swift
//  Panku
//
//  Created by mac on 2017/2/7.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import Alamofire
import ReactiveCocoa
import ReactiveSwift
import Result

class PKNetworkUtils_Alamofire: PKNetworkUtils {
    
    func request(url: String, method: PKHTTPMethod, parameters: [String : Any]?, headers: [String : String]?) -> SignalProducer<Any, NSError> {
        let signalProducer = SignalProducer<Any,NSError> { (observer, disposable) in
            var localMethod:Alamofire.HTTPMethod
            
            switch method {
            case .get:
                localMethod = .get
                break
            case .post:
                localMethod = .post
            default:
                localMethod = .get
            }
            
            Alamofire.request(url, method: localMethod, parameters: parameters, encoding: JSONEncoding.default,headers:headers).responseJSON(completionHandler: { (response) in
                switch response.result {
                case .success:
                    if let json = response.result.value {
                        observer.send(value: json)
                        observer.sendCompleted()
                    }
                    
                case .failure(let error):
                    observer.send(error: error as NSError)
                }
                
            })
            
        }
        
        return signalProducer

    }
    
    static func make() -> PKNetworkUtils {
        return PKNetworkUtils_Alamofire()
    }

 
}
